<?php
// +----------------------------------------------------------------------
// | ThinkBLOG (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author nicholas <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: FormHashBuildBehavior.class.php 105 2012-04-06 07:47:44Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class FormHashBuildBehavior extends Behavior {
	
	protected $options = array ('FORMHASH_ON' => true, 'FORMHASH_NAME' => 'formhash' );
	
	public function run(&$content) {
		if (C ( 'FORMHASH_ON' )) {
			if (strpos ( $content, '{__FORMHASH__}' )) {
				$content = str_replace ( '{__FORMHASH__}', $this->FormHash (), $content );
			} elseif (preg_match ( '/<\/form(\s*)>/is', $content, $match )) {
				$content = str_replace ( $match [0], $this->FormHash () . $match [0], $content );
			}
		}
	}
	
	private function FormHash() {
		$formhashName = C ( 'FORMHASH_NAME' );
		$formhash = '<input type="hidden" name="' . $formhashName . '" value="' . FORMHASH . '" />';
		return $formhash;
	}

}